package com.caifu.controller.drugs;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.caifu.bean.HealthyArchiveSearchBean;
import com.caifu.bean.Result;
import com.caifu.controller.AbstractController;
import com.caifu.pojo.SysUser;
import com.caifu.pojo.YlBuilding;
import com.caifu.pojo.YlFloor;
import com.caifu.pojo.YlUserMedicine;
import com.caifu.service.IBuildingService;
import com.caifu.service.IHealthArchiveService;
import com.caifu.service.YlUserMedicineService;
import com.caifu.util.Util;
import com.caifu.vo.YlUserMedicineVo;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用药管理
 *
 * @author tww
 * ClassName: YlUserMedicineController.java、
 * createTime: 2020年11月11日 13:37:38
 **/
@Controller
@RequestMapping("medicine")
public class YlUserMedicineController extends AbstractController {

    private static final Logger logger = LoggerFactory.getLogger(YlUserMedicineController.class);

    @Resource
    private YlUserMedicineService ylUserMedicineService;

    @Resource
    private IBuildingService iBuildingService;

    @Resource
    private IHealthArchiveService healthArchiveService;

    private String prefix = "medicine/";


    @GetMapping("toMedicinePage")
    public String toMedicinePage(Model model) {
        SysUser user = getUser();
        //楼栋列表
        List<YlBuilding> buildingList = iBuildingService.getBuildingList(user);
        model.addAttribute("buildingList", buildingList);
        return prefix + "drugs_list";
    }


    /**
     * 添加药物
     */
    @PostMapping("addUserMedicine")
    @ResponseBody
    public Result addUserMedicine(YlUserMedicine ylUserMedicine) {
        SysUser user = getUser();
        try {
            return ylUserMedicineService.addUserMedicine(ylUserMedicine, user);
        } catch (Exception e) {
            String msg = "网络异常";
            if (Util.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 根据id查询药物信息
     */
    @GetMapping("getUserMedicineById")
    @ResponseBody
    public Result getUserMedicineById(String id) {
        try {
            YlUserMedicine ylUserMedicine = ylUserMedicineService.getUserMedicineById(id);
            return success(200, "success", ylUserMedicine);
        } catch (Exception e) {
            String msg = "网络异常";
            if (Util.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 根据老人id查询老人药物库
     */
    @GetMapping("getUserMedicineByUserNo")
    @ResponseBody
    public Result getUserMedicineByUserNo(String userNo, Long currentPage, Long pageSize) {
        try {
            IPage<YlUserMedicine> userMedicineIPage = ylUserMedicineService.getUserMedicineByUserNo(userNo, currentPage, pageSize);
            return success(0, "success", userMedicineIPage);
        } catch (Exception e) {
            String msg = "网络异常";
            if (Util.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 根据id删除药物
     */
    @DeleteMapping("deleteById")
    @ResponseBody
    public Result deleteById(String id) {
        try {
            ylUserMedicineService.deleteById(id);
            return success(200, "删除成功");
        } catch (Exception e) {
            String msg = "网络异常";
            if (Util.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 获取楼栋
     */
    @GetMapping("getBuildingList")
    @ResponseBody
    public Result getBuildingList() {
        try {
            SysUser user = getUser();
            List<YlBuilding> buildingList = iBuildingService.getBuildingList(user);
            return success(200, "删除成功", buildingList);
        } catch (Exception e) {
            String msg = "网络异常";
            if (Util.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 获取用量预警、有效期预警
     */
    @GetMapping("getWarningList")
    @ResponseBody
    public Result getWarningList(String flag) {
        SysUser user = getUser();
        try {
            List<List<YlUserMedicineVo>> lists = ylUserMedicineService.getWarningList(flag, user.getCurrentPensionNo());
            return success(200, "success", lists);
        } catch (Exception e) {
            String msg = "网络异常";
            if (Util.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 获取老人信息
     */
    @GetMapping("pagedQueryOldManInfoData")
    @ResponseBody
    public Result pagedQueryOldManInfoData(HealthyArchiveSearchBean bean, Integer pageNum, Integer pageSize) {
        SysUser user = getUser();
        try {
            JSONObject jsonObject = healthArchiveService.pagedQueryOldManInfoData(pageNum, pageSize, user, bean);
            return success(0, "success", jsonObject);
        } catch (Exception e) {
            String msg = "网络异常";
            if (Util.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 通过楼栋号获取楼层
     */
    @GetMapping("getFloor")
    @ResponseBody
    public Result getFloor(String buildNo) {
        try {
            List<YlFloor> floors = ylUserMedicineService.getFloor(buildNo);
            return success(200, "success", floors);
        } catch (Exception e) {
            String msg = "网络异常";
            if (Util.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 跳药物发放、登记列表
     */
    @GetMapping("toGrantPage")
    public String toGrantPage(String flag, String userNo, Model model) {
        SysUser user = getUser();
        model.addAttribute("flag", flag);
        model.addAttribute("userNo", userNo);
        return prefix + "grant_list";
    }


    /**
     * 跳药物发放、登记列表
     */
    @GetMapping("toAddMedicinePage")
    public String toAddMedicinePage(String userNo, Model model) {
        SysUser user = getUser();
        model.addAttribute("userNo", userNo);
        return prefix + "register_drugs";
    }

    /**
     * 跳药物登记详情页
     */
    @GetMapping("toRegDetails")
    public String toMeicineDtials(String id, Model model) {
        model.addAttribute("id", id);
        return prefix + "register_details";
    }

    /**
     * 获取页面头部老人信息
     */
    @GetMapping("getElderlyUserInfo")
    @ResponseBody
    public Result getElderlyUserInfo(String userNo) {
        try {
            JSONObject jsonObject = healthArchiveService.queryElderlyUserInfo(userNo);
            return success(200, "success", jsonObject);
        } catch (Exception e) {
            String msg = "网络异常";
            if (Util.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }



}
